package com.jeomo.sys.provider.web;

import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.dto.ITree;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.util.TreeUtil;
import com.jeomo.shiro.dto.LoginInfo;
import com.jeomo.shiro.util.LoginSessionUtil;
import com.jeomo.sys.provider.request.CreateOrgRequest;
import com.jeomo.sys.provider.request.UpdateOrgRequest;
import com.jeomo.sys.provider.web.vo.OrgTreeVo;
import com.jeomo.sys.provider.web.vo.OrgVo;
import com.jeomo.sys.starter.entity.Org;
import com.jeomo.sys.starter.service.IOrgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 崔贺
 */
@RestController
@RequestMapping("/sys/org")
public class OrgController extends BaseController {

    @Resource
    IOrgService orgService;

    @PostMapping("")
    @ApiOperation("创建组织")
    public CommonResult<Integer> save(@RequestBody CreateOrgRequest orgRequest) {
        Org org = BeanCopyUtil.copyProperties(orgRequest, Org.class);
        if(org.getCode() != null) {
            return CommonResult.success(orgService.updateById(org)? 1 : 0);
        }
        return CommonResult.success(orgService.create(org));
    }

    @PutMapping("/stop/{orgCode}")
    @ApiOperation("停用组织")
    public CommonResult<Integer> stop(@PathVariable("orgCode") String orgCode) {
        orgService.stop(orgCode);
        return CommonResult.success();
    }

    @PutMapping("/start/{orgCode}")
    @ApiOperation("启用组织")
    public CommonResult<Integer> start(@PathVariable("orgCode") String orgCode) {
        orgService.start(orgCode);
        return CommonResult.success();
    }

    @DeleteMapping("/{orgCode}")
    @ApiOperation("删除组织")
    public CommonResult<Boolean> remove(@PathVariable("orgCode") String orgCode) {
        return CommonResult.success(orgService.removeById(orgCode));
    }

    @PutMapping("")
    @ApiOperation("修改组织")
    public CommonResult<Boolean> update(@RequestBody UpdateOrgRequest orgRequest) {
        Org org = BeanCopyUtil.copyProperties(orgRequest, Org.class);
        return CommonResult.success(orgService.updateById(org));
    }


    @GetMapping("/{orgCode}")
    @ApiOperation("根据组织号查询组织详情")
    public CommonResult<OrgVo> queryByCode(@PathVariable("orgCode") String orgCode) {
        Org org = orgService.getById(orgCode);
        return CommonResult.success(BeanCopyUtil.copyProperties(org, OrgVo.class));
    }

    @GetMapping("/tree")
    @ApiOperation("获取所有org, 列出组织树（包含当前登录组织及当前组织下的所有组织）")
    public CommonResult<List<ITree>> tree() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        List<Org> list = orgService.list();
        List<ITree> nodes = BeanCopyUtil.copyListProperties(list, OrgTreeVo::new);
        return CommonResult.success(TreeUtil.buildTree(nodes, LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode(), true));
    }

    @GetMapping("/group/tree")
    @ApiOperation("获取所有组织分组, 如果当前组织是分组，则返回该分组，及以下所有分组信息。 如果当前组织不是分组，则返回父级分组")
    public CommonResult<List<ITree>> groupTree() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        String orgCode = LoginSessionUtil.getCurrentLoginInfo().getOrgCode();
        Org currentOrg = orgService.getById(orgCode);
        String parentCode = null;
        if (currentOrg.isMall()) {
            parentCode = currentOrg.getParentCode();
        } else {
            //如果当前组织是分组，则返回该分组，及以下分组
            parentCode = currentOrg.getCode();
        }
        List<Org> list = orgService.listGroup();
        List<ITree> nodes = BeanCopyUtil.copyListProperties(list, OrgTreeVo::new);
        return CommonResult.success(TreeUtil.buildTree(nodes, parentCode, true));
    }

    @GetMapping("/switch")
    @ApiOperation("获取可的切换组织")
    public CommonResult<List<ITree>> getSwitchOrg() throws IllegalAccessException, NoSuchFieldException, InstantiationException {
        List<Org> list = orgService.list();
        List<ITree> nodes = BeanCopyUtil.copyListProperties(list, OrgTreeVo::new);
        return CommonResult.success(TreeUtil.buildTree(nodes, LoginSessionUtil.getCurrentLoginInfo().getLoginUser().getOrgCode(), true));
    }

    @PutMapping("/switch/{orgCode}")
    @ApiOperation("切换组织")
    public CommonResult<LoginInfo> switchOrg(@PathVariable("orgCode") String orgCode) {
        LoginInfo loginInfo = LoginSessionUtil.setCurrentInfo(orgCode);
        return CommonResult.success(loginInfo);
    }

}
